package com.rizzo.analytics.domain.trend;

import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

import java.io.Serializable;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: jalie
 * Date: 11/23/12
 * Time: 10:04 PM
 * To change this template use File | Settings | File Templates.
 */
public class Trend implements Serializable {

	private static final long serialVersionUID = 9L;

	private List<TrendedService> trendedServices;

	public Trend() {
	}

	public Trend(List<TrendedService> trendedServices) {
		this.trendedServices = trendedServices;
	}

	public List<TrendedService> getTrendedServices() {
		if (trendedServices == null) {
			trendedServices = Lists.newArrayList();
		}
		return trendedServices;
	}

	public void setTrendedServices(List<TrendedService> trendedServices) {
		this.trendedServices = trendedServices;
	}

	public void addTrendedService(TrendedService trendedService) {
		if (trendedServices == null) {
			trendedServices = Lists.newArrayList();
		}
		trendedServices.add(trendedService);
	}

	public TrendedService getTrendedService(final String service) {
		Predicate<TrendedService> isService = new Predicate<TrendedService>() {
			@Override
			public boolean apply(TrendedService trendedService) {
				return (trendedService.getService().equals(service));
			}
		};
		Iterable<TrendedService> filteredService = Iterables.filter(getTrendedServices(), isService);
		return Iterables.getFirst(filteredService, null);
	}

	@Override
	public boolean equals(Object other) {
		return EqualsBuilder.reflectionEquals(other, this);
	}

	@Override
	public int hashCode() {
		return HashCodeBuilder.reflectionHashCode(this);
	}

	@Override
	public String toString() {
		return new ToStringBuilder(this).
				append("trendedServices", trendedServices).
				toString();
	}
}
